package com.mikhailenko.transportMVC.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Convertor {

	public static double getDecimalFromFraction(String fraction) {

		String[] parts = fraction.split("/");
		if (parts.length > 1) {
			double numenator = Double.parseDouble(parts[0].trim());
			double denumenator = Double.parseDouble(parts[1].trim());
			return numenator / denumenator;
		}
		return Double.parseDouble(fraction);
	}

	public static List<String> triangleMasToList(double[][] mas) {
		List<String> result = new ArrayList<String>();

		for (int i = 0; i < mas.length; i++) {
			StringBuilder row = new StringBuilder();

			for (int j = 0; j < mas[i].length; j++) {
				row.append(String.format(Locale.US, "%.2f", mas[i][j]));
				row.append(", ");
			}
			row.deleteCharAt(row.lastIndexOf(","));
			result.add(row.toString());
		}

		return result;
	}

	public static String singleMasToString(double[] mas) {
		StringBuilder result = new StringBuilder();

		for (int i = 0; i < mas.length; i++) {
			result.append(String.format(Locale.US, "%.2f", mas[i]));
			result.append(", ");
		}
		result.deleteCharAt(result.lastIndexOf(", "));
		return result.toString();
	}

	public static List<String> masToList(double[] mas) {
		List<String> result = new ArrayList<String>();

		for (int i = 0; i < mas.length; i++) {
			result.add(String.format(Locale.US, "%.2f", mas[i]));
		}

		return result;
	}
	
	public static List<List<String>> masToList(String[][] mas){
	    List<List<String>> result = new ArrayList<List<String>>();
	    
	    for (int i = 0; i < mas.length; i++){
	        List<String> row = new ArrayList<String>();
	        for (int j = 0; j < mas[i].length; j++){
	            row.add(mas[i][j]);
	        }
	        result.add(row);
	    }
	    
	    return result;
	}
	
	public static List<Integer> listToList(List<String> list){
	    List<Integer> result = new ArrayList<Integer>();
	    
	    for (int i = 0; i < list.size(); i++){
	        result.add(Integer.valueOf(list.get(i)));
	    }
	    
	    return result;
	}
}
